package jiaxa.com.jsbridge.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import jiaxa.com.jsbridge.R;

@ContentView(R.layout.activity_welcome)
public class WelcomeActivity extends AppCompatActivity {

    @ViewInject(R.id.btnSkip)
    private Button btnSkip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();//隐藏标题栏
        x.view().inject(this);

        btnSkip.getBackground().setAlpha(80);

        Message message = new Message();
        message.obj = 5;
        btnSkip.setText("跳过 5秒");
        handler.sendMessageDelayed(message, 1000);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int timer = (int) msg.obj;
            timer--;
            if (timer == 0) {
                finish();
            }
            btnSkip.setText("跳过 "+timer+ "秒");
            msg = Message.obtain();
            msg.obj = timer;
            sendMessageDelayed(msg, 1000);
        }
    };

    /**
     * 跳过按钮*/
    @Event(value = R.id.btnSkip, type = View.OnClickListener.class)
    private void btnSkipOnClickListener(View view){
        finish();
    }

    /**
     * 重写回退按钮*/
    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
}
